题解 AT2018@洛谷 | 2018@Atcoder 【バイナリハックイージー / Unhappy Hacking (ABC Edit)】

C++党题解

P党请右转下楼,那里才是你们的基地

输入是从后面添加,退格是从后面删除。这个feature好像与什么相似……是什么呢?

懂了的点我


好吧,没想到你还没懂。这就是个栈!FILO!

具体操作:

  1. 对于数字键,push对应的数字

  2. 对于退格键,如果文档非空则pop,否则什么都不做。

但是最后的文本是倒着的,怎么办呢?pop进另一个栈!这样就可以正过来了!

代码:

#include <bits/stdc++.h>
using namespace std;

int main()
{
    string oper;
    cin>>oper;
    int l = oper.length();
    stack<char> text, tmp;
    for(int i=0;i<l;i++)
    {
        if(oper[i] != 'B') text.push(oper[i]);
        else
        {
            if(!text.empty()) text.pop();
        }
    }
    while(!text.empty())
    {
        tmp.push(text.top());
        text.pop();
    }
    while(!tmp.empty())
    {
        cout<<tmp.top();
        tmp.pop();
    }
    return 0;
}